import java.net.Socket;


public class ConnexionAgent extends Thread {
	
	private TCP t;
	private String S_position;
	private int[][] pos;
	
	public ConnexionAgent(Socket s,int[][] positions) {
		t = new TCP(s);
		pos = positions;
	}
	
	public void run(){
		
			t.openSession();
			
			S_position=t.lire();   
			int x = Integer.parseInt(S_position.split(",")[0].split("<")[1]);
			int y = Integer.parseInt(S_position.split(",")[1].split(">")[0]);
			int rang = pos[x-1][y-1];
			
			//System.out.println("Joueur "+rang+" "+S_position);
			
			t.ecrire("OK");
			S_position = t.lire();						//On recoit la nouvelle position souhaitee 
			int nx = Integer.parseInt(S_position.split(",")[0].split("<")[1]);
			int ny = Integer.parseInt(S_position.split(",")[1].split(">")[0]);
			//System.out.println("nouvelle position demande "+S_position);
			//si elle est disponible on met a jour
			if (pos[nx-1][ny-1] == 0) {
				pos[x-1][y-1] = 0;
				pos[nx-1][ny-1] = rang;
				t.ecrire("OK");
			}
			//sinon on envoi un message disant que la position est deja occupee
			else {
				pos[x-1][y-1]=rang;
				t.ecrire("NON");
			}
			
			t.closeSession();
	}

}
